﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WPF101_Data
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class BindingHome : Window
    {
        public BindingHome()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            MyData data = this.Resources["myData"] as MyData;
            // button.Background = Brushes.Red;
            if (data != null)
            {
                data.ColorName = Brushes.Red;
                data.SecondColor = Brushes.Azure;
                data.FirstText = "Bind to the first text";
                data.FirstWidth = 100;
                data.SecondText = "Bind text to a string";
                data.Thickness = 15;
            }
            this.DataContext = data;

            data = this.Resources["myDataSource"] as MyData;
            if (data != null)
            {
                data.Name = "Bind to a separate StaticResource";
            }
        }
    }

    // INotifyPropertyChanged interface is required
    public class MyData : INotifyPropertyChanged
    {
        private string name;
        private string firsttext;
        public MyData()
        {
            Name = "Init data";
        }

        public Brush ColorName { get; set; }
        public Brush SecondColor { get; set; }
        public string FirstText
        {
            get
            {
                return firsttext;
            }

            set
            {
                firsttext = value;
                OnPropertyChanged("FirstText");
            }
        }
        public int FirstWidth { get; set; }
        public string SecondText { get; set; }
        public int Thickness { get; set; }
        public string Name
        {
            get 
            { 
                return name; 
            }

            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }


        // This method is also must have. 
        // If you comment out the code here, then you will see the differences.
        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }
}
